package com.haierp.task;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.haierp.model.bi.BiSale;
import com.haierp.model.bi.BiShip;
import com.haierp.service.bi.IBiSaleService;
import com.haierp.service.bi.IBiShipService;
import com.haierp.util.DateUtil;
import com.haierp.util.EnterpriseWxUtil;
import com.haierp.util.ExcelTool.BiExcelHelper;

/**
 * 文件上传
 * @author zhulu
 */
@Component
public class AutoSendMessageTask {
	
	protected Logger logger = LogManager.getLogger(getClass());
	
	@Autowired
	private IBiSaleService biSaleService;
	
	@Autowired
	private IBiShipService biShipService;
	
	/**
	 * 发送近一个月销售报表
	 */
	//每天凌晨00:10:00 执行一次
	//@Scheduled(cron = "0 10 0 ? * *")
    public void sendBiSaleByDate() {
		logger.info("发送销售报表");
    	List<List<Object>> rowDatas = new ArrayList<>();
    	List<BiSale> saleList = biSaleService.selectSaleReportExcelByGmtCreate();
    	if(saleList != null) {
    		for (BiSale biSale : saleList) {
    			List<Object> list = new ArrayList<>();
    	        list.add(DateUtil.ymdFormat(biSale.getDimensionDate()));	                //销售日期
    	        list.add(biSale.getStatisticOuterOrderQuantity());		//主订单数量
    	        list.add(biSale.getStatisticOrderQuantity());		    //子订单数量
    	        list.add(biSale.getStatisticSkuQuantity());	            //sku数量
    	        BigDecimal b = new BigDecimal(biSale.getStatisticSalePrice());
    	        list.add(b.setScale(2,BigDecimal.ROUND_HALF_UP).toString());		        //销售额
    			rowDatas.add(list);
    		}
    	}
    	BiExcelHelper biExcelHelper = new BiExcelHelper();
    	String[] columnTitles = new String[]{"销售日期", "主订单数量", "子订单数量", "sku数量", "销售额"};
    	Integer[] columnWidth = new Integer[]{20, 10, 10, 10, 10};
    	biExcelHelper.setBiSaleToSheet("销售报表", columnTitles, rowDatas, columnWidth);
    	String fileName = "近一个月销售报表";
    	EnterpriseWxUtil.sendMessage(biExcelHelper.writeToByteArrayOutputStream(),fileName);
    	try {
    		biExcelHelper.writeToByteArrayOutputStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
	
	/**
	 * 导出excel
	 */
	//每天凌晨00:15:00 执行一次
	//@Scheduled(cron = "0 15 0 ? * *")
    public void shipReportByDay()  {
		logger.info("发送发货报表");
    	List<List<Object>> rowDatas = new ArrayList<>();
    	List<BiShip> shipList= biShipService.selectBiShipReportExcelByGmtCreate();
    	if(shipList != null) {
    		for (BiShip ship : shipList) {
    			for (int i=0 ; i<2 ; i++) {
    				List<Object> list = new ArrayList<>();	
	    	        list.add(ship.getDimensionDate());	    //时间
	    	        list.add(ship.getStatisticOrderNum());		                //支付订单数
	    	        list.add(ship.getStatisticSkuQuantity());		            //商品总件数
	    	        Long quantity = ship.getCurentDaySku() + ship.getOneDaySku() + ship.getTwoDaySku() + ship.getThreeDaySku() + ship.getFourDaySku() + ship.getFiveDaySku() + ship.getSixDaySku() + ship.getSevenDaySku() + ship.getOtherDaySku();
	    	        	if (i==0){
	    	        		list.add(ship.getStatisticSkuQuantityStockUp());	       //商品备货情况
	    	        		list.add(quantity);		  //商品发货情况
	    	        	} else {
	    	        		list.add(ship.getStatisticSkuQuantityNoStockUp());
	    	        		list.add(ship.getStatisticSkuQuantity() - quantity);
	    	        	}
	    	        rowDatas.add(list);
    			}
    		}
    	}
    	BiExcelHelper biExcelHelper = new BiExcelHelper();
    	String[] columnTitles = new String[]{"时间", "支付订单数", "商品总件数", "商品备货情况", "商品发货情况"};
    	Integer[] columnWidth = new Integer[]{20, 10, 10, 15, 15};
    	biExcelHelper.setBiShipToSheet("发货报表", columnTitles, rowDatas, columnWidth);
    	String fileName = "近一个月发货表格";
    	EnterpriseWxUtil.sendMessage(biExcelHelper.writeToByteArrayOutputStream(),fileName);
    	try {
    		biExcelHelper.writeToByteArrayOutputStream().close();
		} catch (IOException e) {
			e.printStackTrace();
		}
    }
    
}
